home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-02
/
baggrep.zip
/
GREP.CB
< prev
next >
Wrap
Text File
|
1993-03-18
|
12KB
|
370 lines
/****************************************************************************
*
* grep.cb 18Mar93
*
* Search entire set of buffers for string
*
* Contains macros:
* grep
* _show_list
* _get_file_line
* _save_and_exit
*
* Copyright (c) 1990-1993 B. Goldstein -- Pequod Software
*
* Change History (see also individual entries):
* Date Who What
* ------- --- --------------
* 17Apr90 BAG First taken from Brief BBS miscellaneous stuff
* 09Jun90 BAG Added line to show grep search pattern
* 20Jun92 BAG grep has to check search _dir for search_again positioning
* 17Mar93 BAG Added option of preserving grep buffer across calls
* 18Mar93 BAG Changed option so default is saving buffer
*
****************************************************************************/
/* Definitions */
#include <cbrief.h>
/***************************************************************************/
/* Externals */
extern int _reg_exp;
extern string _s_pat;
// save grep buffer across calls
int list_buffer;
string _save_pattern;
// cross-macro "statics"
int _save_grep_list;
int _grep_line;
string _grep_file;
// Prototypes
void grep (void);
void _show_list (int orig_buffer, string pattern);
void _get_file_line (void);
void _save_and_exit (void);
/***************************************************************************/
/****************************************************************************
*
* grep 17Mar93
*
* Takes a search expression and searches all of all the buffers being
* edited
*
* Change History:
* Date Who What
* ------- --- -----------------
* 17Apr90 BAG First draft
* 22Jun92 BAG Slightly better coding
* 17Mar93 BAG Added option of preserving grep buffer across calls
*
****************************************************************************/
void grep (void)
{ int ret_code,
line_at,
end_col,
case_flag,
sta_buffer,
num_bufs,
cur_buffer,
i;
string buf,
full_name,
pattern,
prompt;
/* Initialize for grep */
sta_buffer = inq_buffer ();
save_position (); /* save the current position */
_grep_file = "";
_grep_line = 0;
_save_grep_list = TRUE;
case_flag = search_case (); /* find out what it is */
search_case (case_flag); /* and put it back that way */
// Talk to the user
sprintf (prompt, "Search %s%sall files for: ",
_reg_exp ? "" : "(RE off) ",
case_flag ? "(a=A) " : "");
if (list_buffer != 0)
pattern = ""; // if prior grep, set up for no pattern and re-use
else
pattern = _s_pat;
ret_code = get_parm (0, pattern, prompt, NULL, pattern);
if (! ret_code)
return;
if (pattern == "" && list_buffer == 0)
{ error ("No pattern and no prior grep");
return;
}
/* how many buffers we got? */
cur_buffer = 0;
while (sta_buffer != cur_buffer)
{ ++num_bufs;
cur_buffer = next_buffer ();
set_buffer (cur_buffer);
}
// exits loop with cur_buffer == sta_buffer (original, user's buffer)
if (pattern != "" && list_buffer != 0)
{ delete_buffer (list_buffer);
list_buffer = 0;
}
if (pattern == "") // list_buffer must not be 0
{ pattern = _save_pattern;
message ("Grep'ed earlier for %s", pattern);
}
else // Generate new list buffer
{ list_buffer = create_buffer ("grep list", NULL, SYSTEM_BUFF);
for (i = 0; i < num_bufs; i++)
{ inq_names (full_name, NULL, buf);
message ("Searching %s for %s", buf, pattern);
save_position ();
top_of_buffer (); // start at the top of current user buffer
set_buffer (list_buffer); /* put title in grep buffer */
insert ("\n");
insert (full_name);
insert ("\n\n");
set_buffer (cur_buffer); /* initially, the starting buffer */
/* search for the pattern */
while (search_fwd (pattern, _reg_exp))
{ beginning_of_line ();
inq_position (line_at, NULL);
end_of_line ();
inq_position (NULL, end_col);
/* switch to list buffer */
set_buffer (list_buffer);
/* put in the line number */
insert (" %5d: ", line_at);
/* copy in the line */
transfer (cur_buffer, line_at, 1, line_at, end_col);
/* return to user current buffer */
set_buffer (cur_buffer);
move_abs (++line_at, 1);
}
restore_position (MOVE);
// Allow for quitting after each buffer
if (inq_kbd_char ()) /* see if any keys pressed */
{ read_char (); /* swallow the key */
set_buffer (sta_buffer); // as if we'd finished the loop
i++; // we did finish this file
break;
}
cur_buffer = next_buffer ();
set_buffer (cur_buffer);
}
// Either way out of loop, back at user's original buffer
// (cur_buffer not necessarily set to sta_buffer)
if (i == num_bufs)
message ("Grep'ed %d file%s for %s",
i, ((i == 1) ? "" : "s"), pattern);
else
message ("Grep'ed %d of %d file%s for %s",
i, num_bufs, ((num_bufs == 1) ? "" : "s"), pattern);
}
/* Now that we've searched all of the buffers in memory, */
/* let's display the fruits of our labor. */
_show_list (sta_buffer, pattern);
restore_position (MOVE); /* restore the position */
// See if we're to delete the grep list buffer
if (! _save_grep_list)
{ delete_buffer (list_buffer);
list_buffer = 0;
message ("grep %s done.", pattern);
}
else
{ _save_pattern = pattern;
message ("grep %s done (list saved).", pattern);
}
// Now that we're saving grep results for re-use, the file may be gone
if (_grep_file != "")
{ /* move to that file */
for (i = 0; i < num_bufs; i++)
{ inq_names (full_name, NULL, NULL);
if (full_name == _grep_file)
break;
set_buffer (next_buffer ());
}
if (i == num_bufs) // didn't find it
{ error ("File %s no longer around", _grep_file);
set_buffer (sta_buffer);
}
else
{ attach_buffer (inq_buffer ());
if (_grep_line != 0)
{ move_abs (_grep_line, 1);
if (search_fwd (pattern+"|\\n", _reg_exp) <= 0
|| read (1) == "\\n")
error ("Can't find %s in line %d", pattern, _grep_line);
else
_s_pat = pattern; //@@@ Do we want to do this elsewhere?
}
}
}
}
/****************************************************************************
*
* _show_list 18Mar93
*
* Let user view search results
*
* Change History:
* Date Who What
* ------- --- --------------
* 17Apr90 BAG First draft
* 18Apr90 BAG Changed to overlapping window
* 09Jun90 BAG Added pattern to messages
* 17Mar93 BAG Added option of preserving grep buffer across calls
*
****************************************************************************/
void _show_list (int org_buffer, string pattern)
{ int lines,cols;
inq_screen_size (lines, cols);
create_window (2,lines-5, cols-4,2, "Press Esc to exit, Enter to edit");
set_buffer (list_buffer); /* switch to list buffer */
attach_buffer (list_buffer); /* let's see our handiwork */
top_of_buffer ();
refresh ();
keyboard_push (); /* allow only certain keys */
assign_to_key ("%#73", "page_up"); /* PgUp */
assign_to_key ("%#81", "page_down"); /* PgDn */
assign_to_key ("#27", "_save_and_exit"); /* Esc */
assign_to_key ("<Up>", "scroll_sensitive_up"); /* arrow keys */
assign_to_key ("<Down>", "scroll_sensitive_down");
assign_to_key ("<Left>", "scroll_sensitive_left");
assign_to_key ("<Right>", "scroll_sensitive_right");
assign_to_key ("#20224", "_end"); /* End */
assign_to_key ("#18176", "_home"); /* Home */
assign_to_key ("#13", "_get_file_line"); /* Enter */
process (); /* exit returns to point after this */
keyboard_pop ();
delete_window (); /* get rid of window */
set_buffer (org_buffer); /* return to user's buffer */
attach_buffer (org_buffer); /* get back to normal */
return;
}
/****************************************************************************
*
* _get_file_line 18Mar93
*
* Scans grep list buffer when a line is selected by enter
*
* Shift-Enter means save list buffer
*
* Sets _grep_file and _grep_line for use in main grep routine to
* switch to that file at that line
*
* Change History:
* Date Who What
* ------- --- --------------
* 17Apr90 BAG First draft
* 18Mar93 BAG Look for Shift key to delete grep list buffer
*
****************************************************************************/
void _get_file_line (void)
{ string line_prefix;
int l_prefix;
int line;
if ((inq_kbd_flags () & (LEFT_SHIFT | RIGHT_SHIFT)))
_save_grep_list = FALSE;
beginning_of_line ();
l_prefix = search_fwd ("{< +[0-9]+:}|{\\n}") - 2;
if (l_prefix)
{ line_prefix = read (l_prefix);
_grep_line = atoi (line_prefix);
}
beginning_of_line ();
while (FOREVER)
{ line_prefix = read (1);
if (line_prefix != "\n" && line_prefix != " ")
{ _grep_file = trim (read ());
break;
}
inq_position (line);
if (line == 1)
break;
up ();
}
exit (); /* return to after process call */
}
/****************************************************************************
*
* _save_and_exit 18Mar93
*
* Just like exit (from process viewing grep list) but checks for shift key
* to see if we want to delete the list buffer
*
* Change History:
* Date Who What
* ------- --- -------------
* 18Mar93 BAG First draft
*
****************************************************************************/
void _save_and_exit ()
{
if ((inq_kbd_flags () & (LEFT_SHIFT | RIGHT_SHIFT)))
_save_grep_list = FALSE;
exit ();
}
/**************************** end of grep.cb file **************************/